
package com.kanjian.music.activity;

import java.util.ArrayList;

import com.kanjian.music.R;
import com.kanjian.music.api.ApiRequestManager;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.constants.MusicListTypeConstants;
import com.kanjian.music.entity.Music;
import com.kanjian.music.entity.Musician;
import com.kanjian.music.entity.RankDetail;
import com.kanjian.music.fragment.MusicListFragment;
import com.kanjian.music.fragment.MusicianListFragment;
import com.kanjian.music.network.HttpLoader;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class RankActivity extends BaseActivity implements LoaderCallbacks<String> {

    public int mMusicListType;

    private String mRequestType;

    private Button mBtn_musicList;

    private Button mBtn_musicianList;

    private MusicListFragment mMusicListFragment;

    private MusicianListFragment mMusicianListFragment;

    private ArrayList<Music> mMusicList;

    private ArrayList<Musician> mMusicianList;

    private FragmentTransaction mFragmentTransaction;

    @Override
    protected void onCreate(Bundle arg0) {
        setContentView(R.layout.activity_rank);
        super.onCreate(arg0);
        initView();
        setTitle();
        initLoader();
    }

    private void initView() {
        mMusicListType = getIntent().getIntExtra(IntentConstants.MUSICLISTTYPE,
                MusicListTypeConstants.MUSIC_LIST_TYPE_RECOMMEND);
        mBtn_musicList = (Button) findViewById(R.id.rank_btn_music);
        mBtn_musicianList = (Button) findViewById(R.id.rank_btn_musician);
        mBtn_musicList.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setButtonFocus(mBtn_musicList, mBtn_musicianList);
                toggleFragment(mMusicListFragment, mMusicianListFragment);
            }
        });
        mBtn_musicianList.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setButtonFocus(mBtn_musicianList, mBtn_musicList);
                toggleFragment(mMusicianListFragment, mMusicListFragment);
            }
        });
    }
    
    private void setButtonFocus(Button focusButton, Button otherButton) {
        focusButton.setBackgroundResource(R.drawable.selector_rank_btn_focus);
        focusButton.setTextColor(getResources().getColor(R.color.rank_focus_button_text));
        otherButton.setBackgroundResource(R.drawable.selector_rank_btn);
        otherButton.setTextColor(getResources().getColor(R.color.rank_button_text));
    }

    private void initLoader() {
        Bundle b = new Bundle();
        b.putSerializable(IntentConstants.HTTPLOADERREQUESTPARAM,
                ApiRequestManager.getRankDetailRequest(mRequestType));
        getSupportLoaderManager().initLoader(0, b, this);
    }

    private void toggleFragment(Fragment showFragment, Fragment hideFragment) {
        mFragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (hideFragment != null && hideFragment.isAdded() && !hideFragment.isHidden()) {
            mFragmentTransaction.hide(hideFragment);
        }
        if (showFragment != null) {
            if (!showFragment.isAdded()) {
                mFragmentTransaction.add(R.id.rank_fragment_container, showFragment);
            } else {
                mFragmentTransaction.show(showFragment);
            }
        }
        mFragmentTransaction.commitAllowingStateLoss();
    }

    private void setTitle() {
        String title = null;
        switch (mMusicListType) {
            case MusicListTypeConstants.MUSIC_LIST_TYPE_RECOMMEND:
                title = "你可能喜欢";
                break;
            case MusicListTypeConstants.MUSIC_LIST_TYPE_HOT:
                title = "24小时热门榜";
                mRequestType = RankDetail.TYPE_24H;
                break;
            case MusicListTypeConstants.MUSIC_LIST_TYPE_MONTH:
                title = "最近30天";
                mRequestType = RankDetail.TYPE_30D;
                break;
            case MusicListTypeConstants.MUSIC_LIST_TYPE_WEEK:
                mRequestType = RankDetail.TYPE_7D;
                title = "最近7天";
                break;
            case MusicListTypeConstants.MUSIC_LIST_TYPE_NEW:
                mRequestType = RankDetail.TYPE_NEW;
                title = "新晋榜";
                break;
            default:
                title = "24小时热门榜";
                mRequestType = RankDetail.TYPE_24H;
                break;
        }
        super.setTitle(title);
    }

    @Override
    public Loader<String> onCreateLoader(int id, Bundle args) {
        return new HttpLoader(this, args);
    }

    @Override
    public void onLoadFinished(Loader<String> loader, String data) {
        if (data != null) {
            mMusicList = Music.getMusicListFromJson(data);
            mMusicianList = Musician.getMusicianListFromJson(data);
            mMusicListFragment = MusicListFragment.newInstance(mMusicList);
            mMusicianListFragment = MusicianListFragment.newInstance(mMusicianList);
            toggleFragment(mMusicListFragment, null);
        }
    }

    @Override
    public void onLoaderReset(Loader<String> loader) {
    }

}
